package demo;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.commons.fileupload.*;


//Need the server to redeploy after the client upload a new picture
public class RestaurantRegistration extends HttpServlet {
	
	@SuppressWarnings("deprecation")
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
		boolean isMultipart = FileUpload.isMultipartContent(request);
		PrintWriter out = response.getWriter();
		if (isMultipart){
			DiskFileUpload upload = new DiskFileUpload();
			try {
				List items = upload.parseRequest(request);
				Iterator iter = items.iterator();
				FileItem item = null;
				if (iter.hasNext()) {
				    item = (FileItem) iter.next();	
				}
				
				//For some reason these return null
				//String ImageName = (String)request.getParameter("Image_Name");
				//String ImageName1 = (String)request.getAttribute("Image_Name");
				//out.println(ImageName + ImageName1);
				String FileExtension = "_logo.jpg";
				String RestaurantLoginName = "coffee";
				String ImageName = RestaurantLoginName + FileExtension;
				
				//rnewfile = new File(new File("/").getAbsolutePath()+File.separator+"tomcat"+File.separator+"webapps"+File.separator+"ROOT"+File.separator+"Photo"+File.separator+"YourPhotoname.jpg");
				//absolute path is fine
				File uploadedFile = new File("C:/Users/Cynthia Lu/Documents/MyEclipse 7.0/FoodOrderingSystem/WebRoot/Pics/" + ImageName);
				if (item != null)
					item.write(uploadedFile);
				
				DBConnector db = DBConnector.getDBConnector();
				db.prepareStatement("update restaurant set rest_pic_link = '" + ImageName + "' where rest_login_name = '" + RestaurantLoginName + "'");
				db.updatePstmt();
				
				out.println("Image loading is successful");				
				out.println("<script>window.location.href='../index.jsp';</script>");
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				out.println("Image loading is unsuccessful");
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				out.println("Image loading is unsuccessful");
				e.printStackTrace();
			}

		}
		//return;
	}
	/*
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException , IOException {
		res.setContentType("text/html");
		PrintWriter out = res.getWriter();
		String rtempfile = File.createTempFile("temp","1").getParent();
		MultipartRequest multi = new MultipartRequest(req, rtempfile, 500 * 1024); 
		File rnewfile=null;
		rnewfile = new File(new File("/").getAbsolutePath()+File.separator+"tomcat"+File.separator+"webapps"+File.separator+"ROOT"+File.separator+"Photo"+File.separator+"YourPhotoname.jpg");
		out.println("<HTML>");
		out.println("<head><title>UPLOAD PHOTO</title></head>");
		out.println("<body>");
		out.println("<Pre>");
		Enumeration files = multi.getFileNames();
		
		while (files.hasMoreElements())  {
			File f = multi.getFile(name);
			FileInputStream fin =new FileInputStream(f);
			ImageInfo ii =new ImageInfo();
			ii.setInput(fin);
			fin.close();
		   fin =new FileInputStream(f);
		   FileOutPutStream fos =new  FileOutPutStream(rnewfile);
		   byte sizefile[] = new byte[500000];
		   fin.read(sizefile);
		   fin.write(sizefile);
		   fos.close();
		 
		   fin.close();
		   f.delete();
		}
		res.sendRedirect("Your.jsp");
	}
	*/
}
